import iButton from '@/components/carGoods/i-button.vue'
import iSharePopup from '@/components/i-share-popup.vue'
import { share } from '@/utils/common-api.js'

export default {
  components: { iButton, iSharePopup },
  data() {
    return {
      // 状态为preview：预览，为buyPreview：买家预览
      // 通过此状态判断，如果是买家预览，底部显示加入购物车，立即购买等按钮
      status: 'preview',

      id: 0,
      // 车品详情数据
      detailData: {
        'id': '',
        'goods_name': 'test',
        'goods_Type_Name': 'test',
        'name': '',
        'type_name': '',
        'remark': '',
        'facade_picture': [
          {
            'img': 'test'
          }
        ],
        'original_price': 0,
        'b_price': 0,
        'added_status': 0,
        'added_status_time': '2020-11-30T08:19:11.673Z',
        'goods_type': 0,
        'isdeleted': 0,
        'carlist': [
          {
            'name': '',
            'id': ''
          }
        ],
        'storeData': {
          'id': '',
          'store_Topimg': '',
          'store_Name': '',
          'address': '',
          'goodsCount': '',
          'collection': '',
          'isgoverbool': true
        },
        'send_Region_Name': ''
      },
      carMoreShow: false, // 适配车型查看更多
      goodsSum: 1,
      loading: false, // 加载中
      sharePopupShow: false // 分享弹出显示
    }
  },

  computed: {
    storeHeaderImg() {
      let defaultHeaderImg = this.$store.state.defaultHeaderImg
      let headerImg = this.detailData.storeData.store_Topimg || defaultHeaderImg
      return headerImg
    }
  },

  onLoad(option) {
    this.id = option.id
    this.status = option.status || 'buyPreview'
    this.getCarGoodsDetail()
  },

  // 监听导航栏按钮点击，分享按钮
  onNavigationBarButtonTap(e) {
    console.log(e)
    if (e.type === 'none') {
      this.sharePopupShow = !this.sharePopupShow
    }
  },

  created() {
  },

  methods: {
    // 获取车品详情
    async getCarGoodsDetail() {
      try {
        this.loading = true
        const res = await this.$api.carGoods.GetGoodsCarSupplieDetails({ id: this.id })
        this.detailData = res.data
        console.log('车品详情', res.data)
      } catch (err) {
        //
      }
      this.loading = false
    },

    // 加减改变数量
    updateSum(type) {
      if (type === '+') {
        this.goodsSum++
      } else {
        if(this.goodsSum === 1) return false
        this.goodsSum--
      }
    },

    // 跳转到购物车页面
    goBuyCarPage() {
      uni.navigateTo({
        url: '/pages_order/carGoodsOrder/buyCar/buyCar'
      })
    },

    // 加入购物车
    async addToBuyCar() {
      let data = {
        'buyer_store_id': this.$store.state.userInfoDetail.store.storeId,  // 当前用户storeid
        'store_id': this.detailData.storeData.id,  // 商品所属店铺id
        'car_supplies_id': this.detailData.id,
        'count': this.goodsSum
      }
      console.log(data)
      uni.showLoading()
      try {
        const res = await this.$api.buyCar.AddShoppingTrolley(data)
        console.log(res)
        uni.showToast({ title: '已添加到购物车' })
      } catch (err) {}
      uni.hideLoading()
    },

    // 跳转到提交订单页面
    goSubmitOrderPage() {
      let data = {
        id: this.detailData.id,
        count: this.goodsSum,
        storeData: this.detailData.storeData
      }
      uni.navigateTo({
        url: `/pages_order/carGoodsOrder/submitOrder/submitOrder?data=${JSON.stringify(data)}&tag=buy`
      })
    },

    // 收藏/取消收藏
    async collect() {
      console.log(this.detailData.storeData.id)
      return false
      let data = {
        store_id: this.detailData.storeData.id,
        fans_store_id: this.$store.state.userInfoDetail.store.storeId,
        state: !this.info.isenshrine
      }
      const res = await this.$api.store.UpdateState(data)
      this.$emit('collectChange', res.data)
    },

    // 进店逛逛，跳转到店铺
    goStorePage() {
      uni.navigateTo({
        url: `/pages_common/store/store?id=${this.detailData.storeData.id}`
      })
    },

    // 打电话
    callPhone() {
      console.log(this.detailData)
      uni.makePhoneCall({
        phoneNumber: this.detailData.storeData.phone + '',
      })
    },

    // 点击分享按钮执行
    handleShare(e) {
      // 分享方法
      share({
        type: 0,
        scene: e,
        summary: '超值特惠车品，速抢',
        href: 'https://ztcbs.yzsheng.com/#/pages_carGoods/carGoodsDetail/carGoodsDetail?id=' + this.detailData.id
      }, status => {
        if (status) {
          uni.hideToast() // 关闭生成中提示
        } else {
          uni.showToast({
            title: '分享失败',
            duration: 2000,
            icon: 'none'
          })
        }
      })
    }
  }
}